from datetime import datetime

from django.db.models import Q
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse

from common.models import House, Area
from user_app.models import Order


def detail(request, id):
    if request.method == 'GET':
        house = House.objects.filter(pk=id).first()
        return render(request, 'detail.html', {'house': house})


def search(request):
    # TODO:房间在预订时间内不应该显示
    if request.method == 'GET':
        # 过滤掉自己发布的房源信息
        houses = House.objects.filter(~Q(user_h=request.user)).all()
        areas = Area.objects.all()
        aid = request.GET.get('aid')
        sd = request.GET.get('sd')
        ed = request.GET.get('ed')
        # 过滤订单在该查询时间段并且处于处理状态的房间
        orders = Order.objects.filter(~Q(status__in=["REJECTED", "CANCELED"])).all()
        if aid or (sd and ed):
            if aid:
                area = Area.objects.filter(pk=int(aid)).first()
                houses = area.house_set.filter(~Q(user_h=request.user)).all()
            if sd and ed:
                # 4种时间段情况比较
                orders1 = orders.filter(begin_date__lte=sd, end_date__gte=ed).all()
                orders2 = orders.filter(begin_date__gte=sd, end_date__lte=ed).all()
                orders3 = orders.filter(begin_date__gte=sd, begin_date__lt=ed, end_date__gte=ed).all()
                orders4 = orders.filter(begin_date__lte=sd, end_date__gt=sd, end_date__lte=ed).all()
                orders = []
                orders_list = [orders1, orders2, orders3, orders4]
                for order in orders_list:
                    orders.extend(order)
            for order in orders:
                houses = houses.filter(~Q(pk=order.house_order.id)).all()

            return render(request, 'search.html', {'aid': aid, 'sd': sd, 'ed': ed, 'areas': areas,
                                                   'houses': houses})

        # 过滤订单处于处理状态的house
        for order in orders:
            houses = houses.filter(~Q(pk=order.house_order.id)).all()
        return render(request, 'search.html', {'areas': areas, 'houses': houses})


def booking(request, id):
    if request.method == 'GET':
        house = House.objects.get(pk=id)
        return render(request, 'booking.html', {'house': house})
    if request.method == 'POST':
        begin = request.POST.get('begin_date')
        end = request.POST.get('end_date')
        begin_date = datetime.strptime(begin, "%Y-%m-%d")
        end_date = datetime.strptime(end, "%Y-%m-%d")
        days = (end_date - begin_date).days if (end_date - begin_date).days > 1 else 1
        house = House.objects.get(pk=id)
        house_price = house.price
        amount = days * house_price

        order = Order.objects.create(begin_date=begin_date, end_date=end_date,
                             days=days, house_price=house_price, amount=amount,
                             user_order=request.user, area_order=house.area_h, house_order=house)
        order.house_order.order_count += 1
        order.house_order.save()
        return HttpResponseRedirect(reverse('my:orders'))


